package com.sw.youvote.client;

import java.util.List;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.HTMLTable.Cell;

import com.sw.youvote.client.areas.ContentArea;
import com.sw.youvote.client.areas.FooterArea;
import com.sw.youvote.client.areas.HeadArea;
import com.sw.youvote.client.areas.SidebarArea;
import com.sw.youvote.client.quizwizard.FormularioCreacionArea;
import com.sw.youvote.client.services.QuizServiceDelegate;
import com.sw.youvote.shared.objects.CandidatureBean;
import com.sw.youvote.shared.objects.QuizBean;

public class QuizListGUI {
    /* Constants. */

	public static final int EDIT_LINK = 3;
    public static final int REMOVE_LINK  = 4;

    
    /* PANTALLA */
    protected DockPanel panel;
    
   /* WIDGETS GRIDS */
    protected Label status;
   
    
    /* Data model */
    protected List<QuizBean> quizs;
 
    protected QuizServiceDelegate quizService;
    
    private FormularioCreacionArea formularioCreacionArea;
    private ContentArea contentArea;
    private SidebarArea sidebarArea;
    private HeadArea headArea;
    private FooterArea footerArea;
        
    public void init() {
    	
    	// ADOLFO POR AQUI:
    	// memter this en lso constructores y mantener variable de instancia
    	// para poder desde dentro de las areas iteractuar con la aplicacion
    	// y el resto de areas, subiendo al GUI.
     	headArea = new HeadArea(this);
    	footerArea = new FooterArea();
    	contentArea = new ContentArea(this);
    	sidebarArea = new SidebarArea(this);
    	formularioCreacionArea = new FormularioCreacionArea(this);
    	formularioCreacionArea.getAddCandidatureButton().setVisible(false);
        status = new Label();
        status.setStylePrimaryName("status");
    	placeWidgets();
    }
    
    private void placeWidgets() {
       	// PANEL - PANTALLA
    	panel = new DockPanel();
    	// poner propiedades del panel
    	// panel.setBorderWidth(5);
    	panel.setWidth("100%");
        panel.setStylePrimaryName("dockPanel");
        // poner header en pantalla
    	panel.add(headArea.getPanel(), DockPanel.NORTH);
    	// poner footer en pantalla
    	panel.add(footerArea.getPanel(), DockPanel.SOUTH);
    	// poner boton add new en pantalla
    	panel.add(sidebarArea.getPanel(), DockPanel.EAST);
    	// poner tabla en pantalla
    	panel.add(contentArea.getPanel(), DockPanel.CENTER);
    	RootPanel.get().add(panel);
    }

   
    
    
    
    //public void setCurrentQuiz(QuizBean quiz){
    //	 currentQuiz = quiz;
    //}
    
    public QuizServiceDelegate getQuizService(){
    	return quizService;
    }
    
    public void gui_eventContactGridClicked(Cell cellClicked) {
        int row = cellClicked.getRowIndex();
        int col = cellClicked.getCellIndex();
       
        QuizBean quiz = this.quizs.get(row);
        this.status.setText("Name was " + quiz.getName() + " clicked ");
       
        if (col==EDIT_LINK) {
            this.sidebarArea.getAddNewButton().setVisible(true);
            this.formularioCreacionArea.getUpdateButton().setVisible(true);
            this.formularioCreacionArea.getAddButton().setVisible(false);
            this.formularioCreacionArea.getAddCandidatureButton().setVisible(true);
            //this.descriptionField.setReadOnly(true);
            formularioCreacionArea.loadForm(quiz,2);
        } else if (col==REMOVE_LINK) {
            this.quizService.removeQuiz(quiz);
        }
   }

    

    /* MENSAJES DE EXITO */
    
    public void service_eventAddQuizSuccessful() {
        status.setText("Quiz was successfully added");
        this.quizService.listQuizs();
    }

    public void service_eventUpdateQuizSuccessful() {
        status.setText("Quiz was successfully updated");
        this.quizService.listQuizs();
    }
    public void service_eventRemoveQuizSuccessful() {
        status.setText("Quiz was removed");
        this.quizService.listQuizs();
        
    }

    /* MENSAJES DE ERROR */
    
    public void service_eventUpdateQuizFailed(Throwable caught) {
        status.setText("Update quiz failed");
    }

    public void service_eventAddQuizFailed(Throwable caught) {
        status.setText("Unable to update quiz");
    }

    public void service_eventRemoveQuizFailed(Throwable caught) {
        status.setText("Remove quiz failed");
    }

    public void service_eventListQuizsFailed(Throwable caught) {
        status.setText("Unable to get quiz list");
        
    }
    
    public void service_eventAddCandidatureFailed(Throwable caught) {
		  status.setText("Unable to add candidature");
		
	}

	public void service_eventAddCandidatureSuccessful() {
		status.setText("Candidature was successfully added");
      //this.quizService.listQuizs();
		
	}

	public void service_eventListCandidaturesFailed(Throwable caught) {
		 status.setText("Unable to list candidatures");
		
	}

	public void service_eventListCandidaturesSucess(List<CandidatureBean> result) {
	  this.getFormularioCreacionArea().listCandidatures(result);
      //this.quizService.listQuizs();
		
	}
	
    
    public SidebarArea getSidebarArea(){
    	return sidebarArea;
    }
    
    public ContentArea getContentArea(){
    	return contentArea;
    }
    
    public FormularioCreacionArea getFormularioCreacionArea(){
    	return formularioCreacionArea;
    }
    
    public HeadArea getHeadArea(){
    	return headArea;
    }
    public void setQuizs (List<QuizBean> newQuizs){
    	quizs = newQuizs;
    }
    
    public List<QuizBean> getQuizs(){
    	return quizs;
    }
    
    
}
